package com.wiegand.gui.button {
	import com.caurina.transitions.Tweener;
	import com.wiegand.event.SoundEvent;
	import com.wiegand.sound.ISoundButton;
	import com.wiegand.sound.Soundcontroller;
	import com.wiegand.util.ColorRectangle;
	import com.wiegand.util.SharedObjectManager;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author gandy
	 */
	public class SoundButton extends Sprite implements ISoundButton{
		
		private const _buttonWidth:int=30;
		private const _buttonHeight:int=10;
		private const _barSpace:int=2;
		private const _numOfBars:int=5;
		private const _barWidth:int=int((_buttonWidth-(_barSpace*(_numOfBars-1)))/_numOfBars);
		private const _barHeight:int=int(_buttonHeight/_numOfBars);
		
		private const _alphaActive:Number=1.0;
		private const _alphaInactive:Number=0.65;
		private const _levelAlphaActive:Number=1.0;
		private const _levelAlphaInactive:Number=0.6;
		
		public static var START_VALUE : int = 5;
		
		private var _hit : Sprite;
		private var _bars : Array;
		private var _level : int;
		private var _volume : Number;
		private var _drag : Boolean = false;

		public function SoundButton(startvolume:Number=1) {
			_volume=startvolume;
		}
		
		public function init() : void {
			alpha = _alphaInactive;
			_bars=new Array();
			var foo_x:int=_barWidth;
			for(var i:int=0; i<_numOfBars; i++){
				var bar:Sprite = ColorRectangle.create(0xffffff,1,_barWidth,(_barHeight*(i+1)));
				bar.rotation = 180;
				bar.x = foo_x;
				bar.y = _buttonHeight;
				foo_x = bar.x+bar.width+_barSpace;
				bar.alpha = _levelAlphaInactive;
				addChild(bar);
				_bars.push(bar);
			}
			_hit = ColorRectangle.create(0xff0000, .0, _buttonWidth, _buttonHeight);
			_hit.buttonMode = true;
			addChild(_hit);
			
			_level = (_volume * _numOfBars)/1;
			
			Soundcontroller.getInstance().soundBt = this;
			Soundcontroller.getInstance().updateVolume(_volume);
			Soundcontroller.getInstance().playBgSound("backgroundLoop");
			update(_level);
			
			addListeners();
		}
		
		private function over(e:MouseEvent=null) : void {
			if(!_drag){
				Tweener.removeTweens(this);
				Tweener.addTween(this,{alpha:_alphaActive,time:.5,transition:"linear"});
				for(var i:int=0; i<_numOfBars; i++){
					var bar:Sprite = _bars[i];
					Tweener.removeTweens(bar);
					Tweener.addTween(bar,{height:_buttonHeight,delay:((_numOfBars-i)*.05),time:.5,transition:"easeoutexpo"});
				}
				Soundcontroller.getInstance().playEffSound("soundBtOver");
			}
		}
		
		private function out(e:MouseEvent=null) : void {
			if(!_drag){
				Tweener.removeTweens(this);
				Tweener.addTween(this,{alpha:_alphaInactive,time:.5,transition:"linear"});
				for(var i:int=0; i<_numOfBars; i++){
					var bar:Sprite = _bars[i];
					Tweener.removeTweens(bar);
					Tweener.addTween(bar,{height:(_barHeight*(i+1)),time:.5,transition:"easeoutexpo"});
				}
			}
		}

		private function down(e:MouseEvent) : void {
			over();
			_drag = true;
			addEventListener(Event.ENTER_FRAME, checkLevel);
		}

		private function up(e:MouseEvent) : void {
			_drag = false;
			if( mouseX > -1 && mouseX <= _buttonWidth && mouseY > -1 && mouseY <= _buttonHeight ){
			}else out();
			removeEventListener(Event.ENTER_FRAME, checkLevel);
		}

		private function click(e:MouseEvent) : void {
			checkLevel();
		}
		
		private function checkLevel(e:Event=null):void{
			var level : int = 0;
			for(var i:int=0; i<_numOfBars; i++){
				if( _hit.mouseX >= Sprite(_bars[i]).x - Sprite(_bars[i]).width/2 ) level++;
			}
			if(level!=_level) update(level);
		}
		
		private function update(level:int):void{
			for(var i:int=0; i<_numOfBars; i++){
				if( level > i ) Sprite(_bars[i]).alpha = _levelAlphaActive;
				else Sprite(_bars[i]).alpha = _levelAlphaInactive;
			}
			_level = level;
			_volume = ( 1 / _numOfBars ) * _level;
			dispatchEvent( new SoundEvent( SoundEvent.CHANGE, _level, _volume ) );
			Soundcontroller.getInstance().updateVolume(_volume);
			
			SharedObjectManager.getInstance().saveValue("soundvolume",_volume.toString());
			SharedObjectManager.getInstance().saveValue("soundlevel",_level.toString());
		}
		
		private function addListeners():void{
			_hit.addEventListener(MouseEvent.CLICK, click);
			_hit.addEventListener(MouseEvent.MOUSE_OVER, over);
			_hit.addEventListener(MouseEvent.MOUSE_OUT, out);
			_hit.addEventListener(MouseEvent.MOUSE_DOWN, down);
			_hit.addEventListener(MouseEvent.MOUSE_UP, up);
			stage.addEventListener(MouseEvent.MOUSE_UP, up);		
		}
		
		private function removeListeners():void{
			_hit.removeEventListener(MouseEvent.CLICK, click);
			_hit.removeEventListener(MouseEvent.MOUSE_OVER, over);
			_hit.removeEventListener(MouseEvent.MOUSE_OUT, out);
			_hit.removeEventListener(MouseEvent.MOUSE_DOWN, down);
			_hit.removeEventListener(MouseEvent.MOUSE_UP, up);
			stage.removeEventListener(MouseEvent.MOUSE_UP, up);		
		}
		
		public function get level():int{
			return _level;
		}

		public function get volume():Number{
			return _volume;
		}
		
		public function set level(l:int):void{
			update(l);
		}

		public function set volume(v:Number):void{
			update( Math.round((v*_numOfBars)));
		}
		
		public function lock():void{
			removeListeners();
		}
		
		public function unlock():void{
			addListeners();
		}
		
		public function get value() : Number {
			// TODO: Implement me!!!
			
			return 0;
		}

	}
}
